package com.dyh.controller;

/**
 * @see menuService
 * 引入service层
 */
import com.dyh.service.menuService;
/**
 * @see menu
 * 引入po层
 */
import com.dyh.po.menu;
/**
 * 引入spring组件
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 引入List数据结构
 */
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * menu控制器，用于对功能管理用例进行管理
 */
@Controller
public class menuController {
    /**
     * 使用自动注入标签引用menuService接口及其方法
     */
    @Autowired
    menuService menuservice;
    /**
     * @return String
     * @param model
     * 跳转至功能显示页
     */
    @RequestMapping(value = "menuList")
    public String menulist(Model model) {
        List<menu> menus = menuservice.findAllMenu();
        model.addAttribute("MenuList", menus);
        return "/menu/menulist";
    }

    /**
     * 跳转至功能添加页
     * @return String
     */
    @RequestMapping(value = "menuAdd")
    public String toMenuAdd(){
        return "/menu/menuAdd";
    }
    /**
     * @return String
     * @param model
     * @param request
     * 添加功能动作
     */
    @RequestMapping(value = "menuAddAction")
    public String addMenu(HttpServletRequest request, Model model){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String menuName = request.getParameter("menuname");
        String menuFather = request.getParameter("menufather");
        String menuurl = request.getParameter("menuurl");
        System.out.println("menuname="+menuName+",menuFather="+menuFather+",menuurl="+menuurl);
        int rows = menuservice.addMenu(menuName,menuFather,menuurl);
        if (rows>0){
            return "redirect:/menuList";
        }
        return "menu/menuAdd";
    }
    /**
     * @return ModelAndView
     * @param request
     * 跳转角色修改页
     */
    @RequestMapping(value = "menuEdit")
    public ModelAndView toMenuEdit(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        String mid = request.getParameter("m_id");
        menu menu = menuservice.findMenuById(mid);
        mv.addObject("Menu",menu);
        mv.setViewName("menu/menuEdit");
        return mv;
    }

    /**
     * 修改功能动作
     * @param request
     * @return String
     */
    @RequestMapping(value = "menuEditAction")
    public String editMenu(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String mid = request.getParameter("id");
        String menuname = request.getParameter("menuname");
        String menufather = request.getParameter("menufather");
        String menuurl = request.getParameter("menuurl");
        int row = menuservice.editMenu(mid,menuname,menufather,menuurl);
        if(row>0){
            return "redirect:/menuList";
        }else{
            return "menu/menuEdit";
        }
    }

    /**
     * 删除功能动作
     * @param request
     * @return String
     */
    @RequestMapping(value = "menuDeleteAction")
    public String deleteMenu(HttpServletRequest request){
        String mid=request.getParameter("m_id");
        Long id = Long.valueOf(mid);
        int row = menuservice.deleteMenu(id);
        return "redirect:/menuList";
    }
}
